/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.model;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author natx
 *
 */
public class Orders {
	
	Hashtable<String,Order> htOrders = new Hashtable<String,Order>();

	public boolean orderExists(String code) {
		return htOrders.containsKey(code);
	}
	
	public void replace(Order order) {
		if ( htOrders.containsKey(order.getCode()) ) {
			htOrders.remove(order.getCode());
		}
		try {
			this.add(order);
		} catch (OrderAlreadyExistsException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void add(Order order) throws OrderAlreadyExistsException {
		if ( htOrders.containsKey(order.getCode()) ) throw new OrderAlreadyExistsException();
		htOrders.put(order.getCode(), order);		
	}

	public Order getOrder(String code) throws OrderDoesNotExistException {
		if ( !htOrders.containsKey(code) ) throw new OrderDoesNotExistException();
		return htOrders.get(code);
	}

	public Object[] getOrders() {
		Object[] orders = new Object[htOrders.size()];
		
		Enumeration<Order> enuOrders = htOrders.elements();
		int i = 0;
		while ( enuOrders.hasMoreElements() ) {
			orders[i] = enuOrders.nextElement();
			i++;
		}
		return orders;
	}

	public boolean hasOrders() {
		return !htOrders.isEmpty();
	}

}
